package com.zy.service.bs.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.dao.bs.BbsDao;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.bs.Bbs;
import com.zy.pojo.query.bs.BbsQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.BbsService;
import com.zy.utils.DateUtil;

@Service("bbsService")
public class BbsServiceImpl extends BaseServiceImpl<Bbs> implements BbsService {

	@Autowired
	private BbsDao bbsDao;

	@Override
	public PageVo pageBbs(BbsQuery bbsQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.bbsDao.countBbs(bbsQuery), this.bbsDao.listBbs(bbsQuery, start, limit));
	}

	@Override
	public void saveBbs(Bbs bbs) {
		long time = DateUtil.getTime();
		bbs.setCreateTime(time);
		bbs.setModifyTime(time);
		bbs.setStatus(StatusEnum.NORMAL.getValue());
		this.bbsDao.save(bbs);
	}
	
	@Override
	public void updateBbs(Bbs bbs) {
		bbs.setModifyTime(DateUtil.getTime());
		this.bbsDao.updateByPrimaryKeySelective(bbs);
	}

	@Override
	public void removeBbs(long id) {
		try {
			this.bbsDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			Bbs bbs = new Bbs();
			bbs.setId(id);
			bbs.setModifyTime(DateUtil.getTime());
			bbs.setStatus(StatusEnum.DELETE.getValue());
			this.bbsDao.updateByPrimaryKeySelective(bbs);
		}
	}

	@Override
	public void recover(long id) {
		Bbs bbs = new Bbs();
		bbs.setId(id);
		bbs.setModifyTime(DateUtil.getTime());
		bbs.setStatus(StatusEnum.NORMAL.getValue());
		this.bbsDao.updateByPrimaryKeySelective(bbs);
	}

	@Override
	public PageVo appBbs(BbsQuery bbsQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.bbsDao.appCountBbs(bbsQuery), this.bbsDao.appListBbs(bbsQuery, start, limit));
	}

}
